<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/11/7
 * TIME: 18:38
 */

namespace app\index\model;

use think\Db;
use Think\Exception;
use think\facade\Cache;

class Article extends BaseModel {

    public static function RecommendForyou(){
        if(Cache::has('index')){
            $arr = Cache::get('index_foryou');
        }else{
            $arr = self::order('hits', 'desc')->limit(6)->cache('index_foryou', config('cache_time'))->select();
        }

        return $arr;
    }

    public static function getArticleById($id){
        /**记录1点击量***/
        Db::name('article')->where('id', $id)->setInc('hits');
        /**************/

        if(Cache::has('article_'.$id)){
            return Cache::get('article_'.$id);
        }else{
            $arr = self::alias('a')
                ->join('itbus_article_text b', 'a.id=b.cid')
                ->where('a.id',$id)
                ->cache('article_'.$id, config('cache_time'))
                ->find();
            return $arr;
        }
    }

    public static function getTags() {
        if(Cache::has('tags')){
            $tags = Cache::get('tags');
        }else{
            $tags = Db::name('tags')->cache('tags', config('cache_time'))->select();
        }
        return $tags;
    }

    public static function bestnews(){
        if(Cache::has('index_best')){
            $arr = Cache::get('index_best');
        }else{
            $arr = self::order('createtime', 'desc')->limit(6)->cache('index_best', config('cache_time'))->select();
        }

        //echo self::getLastSql();
        return $arr;
    }

    /** 查评论
     * @param $id 文章id
     * @return array
     */
    public static function comm($id){

        if(Cache::has('article_comm_'.$id)){
            $comm = Cache::get('article_comm_'.$id);
        }else{
            $comm = Db::name('comment')->where('aid', $id)->cache('article_comm_'.$id, config('cache_time'))->select();
        }
        $count = 0; //评论数
        foreach($comm as $k=>$v){
            $count = $k + 1;
            if(Cache::has('article_reply_'.$v['id'])){
                $reply = Cache::get('article_reply_'.$v['id']);
            }else{
                $reply = Db::name('reply')
                    ->alias('a')
                    ->field(['a.*','b.id as bid','b.username as to_username'])
                    ->join('itbus_user b', "a.comment_id =  {$v['id']} and a.to_id=b.id")
                    ->cache('article_reply_'.$v['id'], config('cache_time'))
                    ->select();
            }

            if(!empty($reply)){
                $comm[$k]['reply'] = $reply;
            }
        }

        $comm['count'] = $count;
        return $comm;
    }

    public static function addArticle($data){
        try{
            $article = [
                'title'=>$data['title'],
                'lm'=>$data['tags'],
                'flm'=>'xx',
                'author'=>session('username'),
                'createtime'=>date('Y-m-d H:i:s', time()),
            ];
            Db::name('article')->data($article)->insert();
            $id = Db::getLastInsID();

            $text = [
                'id'=>$id,
                'cid'=>$id,
                'content'=>$data['content']
            ];
            Db::name('article_text')->data($text)->insert();
        }catch(\Exception $e){
            return false;
        }
        return true;
    }

    public static function img_up(){

    }

    public static function article_list($tag){
        if($tag == 'node')
            $tag = 'node.js';
        $list = self::where('status', 1)
            ->where('lm', $tag)
            ->order('createtime', 'desc')
            ->paginate(20);
        return $list;
    }
}